<?php
require("dbconfig.php");
require("functions.php");
require("./class/sqlHelper.php");
$db = new Opmysqli(HOST, USER, PASSWORD, DBNAME);
?>
<!doctype html>
<html><head>
<meta charset="utf-8">
<title>修改商品的逻辑处理</title>   
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link href="./bootstrap/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
body{
    font-family:微软雅黑;
    font-size:16px;
    color:red;
    }
#color_c
{
    color:black;
}
.div_c
{
    margin-top:30px;
}
</style>
<!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
    <div class="well div_c">
<?php
if($resError = $db->msg_error())
{
    echo $resError;
    exit(0);
}

//获取要修改的信息
$goodsId = $_POST["goodsId"];
$goodsName = $_POST["goodsName"];
$price = $_POST["price"];
//165M
$MQuantity = $_POST["MQuantity"];
$MLongth = $_POST["MLongth"];
$MWidth = $_POST["MWidth"];
//170L
$LQuantity = $_POST["LQuantity"];
$LLongth = $_POST["LLongth"];
$LWidth = $_POST["LWidth"];
//175XL
$XLQuantity = $_POST["XLQuantity"];
$XLLongth = $_POST["XLLongth"];
$XLWidth = $_POST["XLWidth"];
//180(XXL)
$XXLQuantity = $_POST["XXLQuantity"];
$XXLLongth = $_POST["XXLLongth"];
$XXLWidth = $_POST["XXLWidth"];
//185(XXXL)
$XXXLQuantity = $_POST["XXXLQuantity"];
$XXXLLongth = $_POST["XXXLLongth"];
$XXXLWidth = $_POST["XXXLWidth"];
//商品描述
$desc = $_POST["desc"];

//码，隐藏域的值
$s165 = $_POST["s165"];
$s170 = $_POST["s170"];
$s175 = $_POST["s175"];
$s180 = $_POST["s180"];
$s185 = $_POST["s185"];

$pic = $_POST["oldpic"];

$total = $MQuantity + $LQuantity + $XLQuantity + $XXLQuantity + $XXXLQuantity;
$arr1 = array(
    "商品ID" => array($goodsId),
    "165M" => array($MQuantity, $MLongth, $MWidth),
    "170L" => array($LQuantity, $LLongth,  $LWidth),
    "175XL" => array($XLQuantity, $XLLongth, $XLWidth),
    "180XXL" => array($XXLQuantity, $XXXLLongth, $XXXLWidth),
    "185XXXL" => array($XXXLQuantity, $XXXLLongth, $XXXLWidth)
            );
$arr2 = array("商品名称" => $goodsName, "商品描述" => $desc);

  //2、验证
$res = checkValidate($arr1, $arr2, $price);
   
if ($res["error"])
{
    //3、判断有无图片上传
    if ($_FILES['pic']['error'] != 4)
    {
        //说明有上传的图片，得执行上传
        $upinfo = uploadFile("pic", "./uploads");
        if ($upinfo["error"] === false)
        {
            die("图片信息上传失败！".$upinfo["info"]);
        }
        else
        {
            //上传成功的话
             $pic = $upinfo["info"];   //获取上传的图片名
             $pic1 = $pic;
             //4、执行图片缩放
             imageUpdateSize("./uploads/".$pic, 100, 100, $pre = "s_");
        }
    }

     //5、执行修改
    //sql语句，并执行添加
$sql = "update clothes set name='{$goodsName}', price={$price}, 
        total={$total}, pic='{$pic}', note='{$desc}' where id={$goodsId};";

$sql .= "update clothes_parameter set longth={$MLongth}, width={$MWidth},
         quantity={$MQuantity} where id={$goodsId} and sizeCode='{$s165}';";

$sql .= "update clothes_parameter set longth={$LLongth}, width={$LWidth},
        quantity={$LQuantity} where id={$goodsId} and sizeCode='{$s170}';";

$sql .= "update clothes_parameter set longth={$XLLongth}, width={$XLWidth},
        quantity={$XLQuantity} where id={$goodsId} and sizeCode='{$s175}';";
        
$sql.="update clothes_parameter set longth={$XXLLongth}, width={$XXLWidth},
    quantity={$XXLQuantity} where id={$goodsId} and sizeCode='{$s180}';";

$sql.="update clothes_parameter set longth={$XXXLLongth},width={$XXXLWidth},
    quantity={$XXXLQuantity} where id={$goodsId} and sizeCode='{$s185}';";

    $insertRes = $db->mysqli_multi_dml($sql);

    if($insertRes == 1)
    {
        if ($_FILES['pic']['error'] != 4)
        {
            //删除图片
            @unlink("./uploads/".$_POST['oldpic']);
            @unlink("./uploads/s_".$_POST['oldpic']);
        }
        echo "<span id=\"color_c\">修改衣服商品成功！</span>";
    }
    else
    {
       //如果数据库执行sql语句失败，如果有图片上传的话，会上传相应的图片
        if ($_FILES['pic']['error'] != 4)
        {
            //删除图片
            @unlink("./uploads/".$pic1);
            @unlink("./uploads/s_".$pic1);
        }
        echo "修改衣服商品失败！".$insertRes;
        exit(0);
    }
}
else
{
    echo $res["info"];
}


//关闭数据库
$db->close_conn();

?>
</div>
</div>
<script src="./bootstrap/js/bootstrap.min.js"></script>
<script src="./js/jquery-1.9.1.min.js"></script>
 </body>
 </html>
